﻿using huqiang.Communication;
using huqiang.DataSet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace huqiang.ProtoDataPro
{
    public class QueryCmd
    {
        /// <summary>
        /// 查询本机的外网地址
        /// </summary>
        public const UInt16 SelfAddress = 0xff00;
        public const UInt16 SelfAddressResult = 0xff01;
        /// <summary>
        /// 获取服务器当前所有连接用户
        /// </summary>
        public const UInt16 AllLinks = 0xff02;
        public const UInt16 AllLinksResult = 0xff03;
    }
    public class ServerQueryDataPro
    {
        public static void Handle(KcpUser link, int cmd, byte[] dat)
        {
            int type = cmd >> 8;
            switch(type)
            {
                case 255:
                    SuperQuery(link,cmd, dat);
                    break;
            }
        }
        static void SuperQuery(KcpUser link, int cmd, byte[] dat)
        {
            switch(cmd)
            {
                case QueryCmd.SelfAddress:
                    IPAddr address = new IPAddr();
                    address.IP = link.ip;
                    address.port = (UInt16)link.port;
                    dat = ProtoSerializer.Serialize(address);
                    link.Send(EnvelopeType.ProtoBuf,QueryCmd.SelfAddressResult,dat,MsgType.ServerQuery);
                    break;
                case QueryCmd.SelfAddressResult:
                    address = ProtoSerializer.Deserialize<IPAddr>(dat);

                    break;
                case QueryCmd.AllLinks:
                    //List<NetUser> users = new List<NetUser>();
                    //NetServer.Instance.kcpServer.GetAllLinks(users);
                    //StringBuilder builder = new StringBuilder();
                    //for (int i = 0; i < users.Count; i++)
                    //{
                    //    if (i > 0)
                    //        builder.Append('\n');
                    //    builder.Append(users[i].endPoint);
                    //    if (users[i].Tag != null)
                    //    {
                    //        builder.Append(' ');
                    //        builder.Append(users[i].Tag);
                    //    }
                    //}
                    //StringContent content = new StringContent();
                    //content.Text = builder.ToString();
                    //dat = ProtoSerializer.Serialize(content);
                    //link.Send(EnvelopeType.ProtoBuf,QueryCmd.AllLinksResult,dat,MsgType.ServerQuery);
                    break;
                case QueryCmd.AllLinksResult:
                    //content = ProtoSerializer.Deserialize<StringContent>(dat);

                    break;
            }
        }
    }
}
